<?php
namespace Kuoqi\Api\Weixin\Mp;
use Kuoqi\Api\Weixin\Tools;
use Kuoqi\Api\Weixin\Config;
/**
 * 服务模块的相关信息
 * Class Server
 * @package Kuoqi\Api\Weixin\mp
 */
class Server{

    /**
     * 当前微信号
     * @var string
     */
    public $wx_id = '';
    /**
     * 当前用户的openid
     * @var string
     */
    public $openid = '';
    /**
     * 服务对象的初始化
     * Server constructor.
     */
    public function __construct()
    {
        //var_dump(Config::$mp_appid,Config::$mp_secret);exit();
        //cache('test','123456');
    }

    /**
     * 获取xml文本
     */
    public function getXml(){
        return file_get_contents('PHP://input');
    }

    //获取xml文件并识别为对象
    public function xmlToArray($xml){
        return Tools::xmlToArray($xml);
    }

    //loadConfig-加载收到的消息的配置信息
    public function loadConfig($configs){
        $this->wx_id = $configs['ToUserName'];
        $this->openid = $configs['FromUserName'];
    }

    //空响应
    public function responseEmpty(){
        return 'success';
    }

    //文本内容响应
    public function responseText($text){
        $array = array(
            'MsgType'=>'text',
            'Content'=>$text,
        );
        return $this->resultXml($array);
    }

    //回复xml信息
    public function resultXml($array = array()){
        $array['ToUserName'] = $this->openid;
        $array['FromUserName'] = $this->wx_id;
        $array['CreateTime'] = time();
        $xml = '';
        $arr = array();
        foreach($array as $k=>$v){
            $arr[] = "<{$k}><![CDATA[{$v}]]></{$k}>";
        }
        return '<xml>'.implode('',$arr).'</xml>';
    }

}